﻿using AnQinFrameWorkAPI.Entity.BaseInfo.Entity;
using AnQinFrameWorkAPI.Entity.Blog.Entity;
using AnQinFrameWorkAPI.Entity.Develop.Entity;
using AnQinFrameWorkAPI.Entity.SystemCore.Entity;
using Microsoft.EntityFrameworkCore;

namespace AnQinFrameWorkAPI.DataBase;

public class DatabaseContext : DbContext
{
    /// <summary>
    ///     构造函数
    /// </summary>
    public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        #region 系统

        #region 菜单

        modelBuilder.Entity<MenuModel>(entity =>
        {
            entity.HasOne(s => s.Menu)
                .WithMany(m => m.Children)
                .HasForeignKey(a => a.SuperiorId)
                .IsRequired(false);
        });

        modelBuilder.Entity<MenuButtonModel>(entity =>
        {
            entity.HasOne(b => b.Menu)
                .WithMany(m => m.MenuButtonList)
                .HasForeignKey(b => b.MenuId)
                .IsRequired(false);
        });

        modelBuilder.Entity<MenuFieldModel>(entity =>
        {
            entity.HasOne(b => b.Menu)
                .WithMany(m => m.MenuFieldList)
                .HasForeignKey(b => b.MenuId)
                .IsRequired(false);
        });

        #endregion

        #region 编码规则

        modelBuilder.Entity<CodeRuleItemModel>(entity =>
        {
            entity.HasOne(b => b.CodeRule)
                .WithMany(m => m.CodeRuleItemList)
                .HasForeignKey(m => m.CodeRuleId)
                .IsRequired(false)
                .OnDelete(DeleteBehavior.Cascade);
        });

        #endregion

        #endregion

        #region 基础

        #region 员工

        modelBuilder.Entity<StaffRoleModel>(entity =>
        {
            entity.HasOne(s => s.Staff)
                .WithMany(m => m.StaffRoleList)
                .HasForeignKey(s => s.StaffId)
                .IsRequired(false)
                .OnDelete(DeleteBehavior.Cascade);
        });

        #endregion

        #region 部门

        modelBuilder.Entity<DepartmentModel>(entity =>
        {
            entity.HasOne(s => s.Department)
                .WithMany(m => m.Children)
                .HasForeignKey(a => a.SuperiorId)
                .IsRequired(false);
        });

        #endregion

        #endregion
    }

    #region 系统

    #region 菜单

    //菜单模型
    public DbSet<MenuModel> Menu { get; set; }

    //菜单按钮模型
    public DbSet<MenuButtonModel> MenuButton { get; set; }

    //菜单字段模型
    public DbSet<MenuFieldModel> MenuField { get; set; }

    #endregion

    #region 编码规则

    //编码规则表
    public DbSet<CodeRuleModel> CodeRule { get; set; }

    //编码规则子单表
    public DbSet<CodeRuleItemModel> CodeRuleItem { get; set; }

    #endregion

    #region 全局配置

    //全局配置表
    public DbSet<SystemConfigModel> SystemConfig { get; set; }

    #endregion

    #region 角色

    //角色模型
    public DbSet<RoleModel> Role { get; set; }

    //角色菜单模型
    public DbSet<RoleMenuModel> RoleMenu { get; set; }

    //角色按钮模型
    public DbSet<RoleButtonModel> RoleButton { get; set; }

    //角色字段模型
    public DbSet<RoleFieldModel> RoleField { get; set; }

    #endregion

    #endregion

    #region 基础

    #region 员工

    //员工表
    public DbSet<StaffModel> Staff { get; set; }

    //员工角色表
    public DbSet<StaffRoleModel> StaffRole { get; set; }

    #endregion

    #region 部门

    //部门表
    public DbSet<DepartmentModel> Department { get; set; }

    #endregion

    #endregion

    #region 博客

    #region 博客菜单

    //博客菜单模型
    public DbSet<BlogMenuModel> BlogMenu { get; set; }

    #endregion

    #region 博客登录

    //博客登录模型
    public DbSet<BlogLoginModel> BlogLogin { get; set; }

    #endregion

    #region 发展历程

    //博客发展历程模型
    public DbSet<BlogDevelopHistoryModel> BlogDevelopHistory { get; set; }

    #endregion

    #region 博客评论

    //博客评论模型
    public DbSet<BlogCommentModel> BlogComment { get; set; }

    #endregion

    #region 博客全局配置

    //博客全局配置模型
    public DbSet<BlogGlobalConfigModel> BlogGlobalConfig { get; set; }

    #endregion

    #region 博客项目案例

    //博客项目案例模型
    public DbSet<BlogProjectCaseModel> BlogProjectCase { get; set; }

    #endregion

    #region 博客团队成员

    //博客团队成员模型
    public DbSet<BlogTeamMemberModel> BlogTeamMember { get; set; }

    #endregion

    #region 博客文章

    //博客文章模型
    public DbSet<BlogTextModel> BlogText { get; set; }

    #endregion

    #region 博客用户

    //博客用户模型
    public DbSet<BlogUserModel> BlogUser { get; set; }

    #endregion

    #endregion

    #region 研发

    #region 开发管理客户

    //开发管理客户模型
    public DbSet<DevelopProjectCustomerModel> DevelopProjectCustomer { get; set; }

    #endregion

    #region 开发管理项目列表

    //开发管理项目模型
    public DbSet<DevelopProjectListModel> DevelopProjectList { get; set; }

    //开发管理项目需求模型
    public DbSet<DevelopProjectDemandModel> DevelopProjectDemand { get; set; }

    #endregion

    #region 开发管理项目管理

    //开发管理项目管理模型
    public DbSet<DevelopProjectManagerModel> DevelopProjectManager { get; set; }

    #endregion

    #endregion
}